<?php
// defined('IN_ZEEGING') or die('Access Denied');
ini_set('display_errors', 'on');
class ApiController{
	
	//私有变量
	
	private $devDomain="http://apikit.bybrain.cn";
	private $liveDomain="";
	private $model="DEV";
	
    //参数1：访问的URL，参数2：post数据(不填则为GET)，参数3：提交的$cookies,参数4：是否返回$cookies
    protected function curl_request($url,$post='',$cookie='', $returnHeader=1){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)');
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
        curl_setopt($curl, CURLOPT_REFERER, "http://XXX");
        if($post) {
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post));
        }
        if($cookie) {
            curl_setopt($curl, CURLOPT_COOKIE, $cookie);
        }
        curl_setopt($curl, CURLOPT_HEADER, $returnHeader);
        curl_setopt($curl, CURLOPT_TIMEOUT, 30);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $data = curl_exec($curl);
        if (curl_errno($curl)) {
            return curl_error($curl);
        }
        $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
        curl_close($curl);
        // echo $data;
        if($returnHeader){

            list($header, $body) = explode("\r\n\r\n", $data, 2);
            // 这里处理当POST请求>1k的情况回返回一个100 Continue状态, 会在订单接口出现，因为订单数据比较大
            if($header=="HTTP/1.1 100 Continue") list($header, $body) = explode("\r\n\r\n", $body, 2);
            preg_match_all("/Set\-Cookie:([^;]*);/", $header, $matches);
            $info['cookie']  = isset($matches[1][0])?substr($matches[1][0], 1):null;
            $info['content'] = $body;
            $info['httpheader'] = explode("\r\n", $header);
            $info['httpcode'] = $httpCode;
            return $info;
        }else{
            return $data;
        }
    }

	public function indexAction(){
		print_r('ERROR');
	}

	private function formatPostData($post_data){
		$o="";
		foreach ( $post_data as $k => $v ) 
        { 
            $o.= "$k=" . urlencode( $v ). "&" ;
        }
        return substr($o,0,-1);
	}
    protected function GET($key=NULL){
        if(isset($key)){
            return isset($_GET[$key]) ? $_GET[$key] : NULL;
        }
        else{
            return $_GET;
        }
    }
    protected function POST($key=NULL){
        if(isset($key)){
            return isset($_POST[$key]) ? $_POST[$key] : NULL;
        }
        else{
            return $_POST;
        }
    }
    protected function REQUEST($key){
        return isset($_REQUEST[$key]) ? $_REQUEST[$key] : NULL;
    }

    protected function getPath(){
        if(isset($_SERVER['PATH_INFO'])){
            return $_SERVER['PATH_INFO'];
            // return $path=trim($_SERVER['PATH_INFO'],'/');
        }
        else{
            return null;
        }
    }
	
	public function routerAction(){
		//$betCount=50;//注数
		$param=$this->POST(); //简单验证，后续需要完善容错
		$path=$this->getPath();
        $query=$this->GET();
        $queryStr=$this->formatPostData($query);
        if(!empty($query)){
            $apiUrl=$path.'?'.$queryStr;
        }
        else{
            $apiUrl=$path;
        }
        
        $serverName = $this->model=='LIVE' ? $this->liveDomain : $this->devDomain;

        $fullUrl=$serverName.$apiUrl;
        //$url='http://www.multilotto.com/'
        //$post_data['currency']='EUR';
        // print_r($apiUrl);
        // print_r($fullUrl);
        // exit();
        if(!empty($param)){
            $res= $this->curl_request($fullUrl,$param,1);
        }
        else{
            $res = $this->curl_request($fullUrl,'',1); 
        }
		
              
        //print_r($url);
        header($res['httpheader'][0]);
		print_r($res['content']);
        // print_r($res);
	}
	
}

$api=new ApiController();
$api->routerAction();
//------------------------------------------------
//End of File : /Api.php
//------------------------------------------------